const Base = require('./base.js');

module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.agency = this.model("system/agency");
		this.agency_level = this.model("sys_agency_level");
	}
	async indexAction() {
		if(this.isGet) {
			let levels = await this.agency_level.select();
			
			console.log(levels);
			this.assign("agencyLevels",levels);
			return this.display();
		}

		let posts = this.post();
		let where = {};
		if(!think.isEmpty(posts.level)) {
			where.level = posts.level;
		}
		let fields = "id, level, status, user_id, create_time";
		const data = await this.agency.joinUserTable(posts,where,fields);
		return this.json(data);


	}
	
	async deleteAction() {

		let id = this.post("id");
		let rows = await this.agency.where({
			id: id
		}).delete();
		if(rows == 0) {
			return this.fail("删除失败");
		}
		return this.success("删除成功");

	}
	
	async statusAction() {

		let id = this.post("id");
		let status = this.post("status");
		let row = await this.agency.setOne(id, {
			status: status
		});

		if(row == 0) {
			return this.fail("修改失败");
		}

		return this.success();

	}
	
	async editAction() {

		let posts = this.post();
		let row = await this.agency.setOne(posts.id, {
			level: posts.level
		});

		if(row == 0) {
			return this.fail("修改失败");
		}

		return this.success();

	}
};